package com.qianfeng.smartdevice.controller;

import com.qianfeng.smartdevice.dto.R;
import com.qianfeng.smartdevice.dto.UserDto;
import com.qianfeng.smartdevice.pojo.SysUserInfo;
import com.qianfeng.smartdevice.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * Created by wenka on 2022/3/14 17:12
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */

@RestController
@RequestMapping("/sysUser")
public class SysUserController {
    private SysUserService sysUserService;

    @Autowired

    public void setSysUserService(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @GetMapping("/findById/{id}")
    public R findById(@PathVariable Long id) {
        SysUserInfo byId = sysUserService.findById(id);
        return R.setOK(byId);
    }
    @PostMapping("/registry")
    public R registry(@RequestBody UserDto userDto){
        SysUserInfo userInfo = new SysUserInfo();
        //拷贝对象将相同的属性内容复制到SysUserInfo
        BeanUtils.copyProperties(userDto,userInfo);
        sysUserService.addUser(userInfo);
        return R.steOK();
    }
    @PostMapping("/login")
    public R login(@RequestBody UserDto userDto, HttpSession session){
        SysUserInfo userInfo = sysUserService.login(userDto);
        session.setAttribute("user",userInfo);
        return R.steOK();


    }
}
